﻿#define _CRT_SECURE_NO_WARNINGS
#define PI 3.14
#include <stdio.h>
#include<math.h>
//////int main()
//////{
//////	int a = 10;
//////	void* pa = &a;
//////	void* pc = &a;
//////	*pa = 10;
//////	*pc = 0;
//////	return 0;
//////
//////	return 0;
//////}
////
////
//////int main()
//////{
//////	int flag = -1;
//////	int i = 0;
//////	double sum = 1;
//////	for (i = 2; i <= 100; i++)
//////	{
//////		sum += flag * 1.0 / i;
//////		flag *= -1;
//////	}
//////	printf("%.3lf", sum);
//////	return 0;
//////}
////
////
////
//////int main()
//////{
//////	int i = 0;
//////	int sum = 0;
//////	for (i = 1; i <= 1000; i++)
//////	{
//////		int j = 0;
//////		for (j = 1,sum=0; j < i; j++)
//////		{
//////
//////			if (i % j == 0)
//////			{
//////				sum += j;
//////			}
//////		}
//////		if(sum==i)
//////		printf("%d ", sum);
//////	}
//////
//////	return 0;
//////}
//////
////
////
//////int main()
//////{
//////	int i = 0;
//////	for (i = 100; i < 1000; i++)
//////	{
//////		int count = 0;
//////		int sum = 0;
//////		int tmp = i;
//////		while (tmp)
//////		{
//////			count++;
//////			tmp /= 10;
//////		}
//////		tmp = i;
//////		while (tmp)
//////		{
//////			sum += pow((tmp % 10), count);
//////			tmp /= 10;
//////		}
//////		if (sum == i)
//////		{
//////			printf("%d ", i);
//////		}
//////	}
//////}
////
////
////
//////int main()
//////{
//////	int i = 0;
//////	int n = 0;
//////	int sum = 0;
//////	int m = 1;
//////	scanf("%d", &n);
//////	for (i = 1; i <= n; i++)
//////	{
//////		m *= i;
//////		sum += m;
//////	}
//////	printf("%d", sum);
//////}	
////	
////	
////
////
////
////
////
//////int main()
//////{
//////	int n = 0;
//////	int sum = 1;
//////
//////	scanf("%d", &n);
//////	while (n)
//////	{
//////		sum *= (n % 10);
//////		n /=10;
//////	}
//////	printf("%d", sum);
//////	return 0;
//////}
////
////
////
//////int main()
//////{
//////	float sum = 0;
//////	int i = 0;
//////	int n = 0;
//////	float score = 0;
//////	scanf("%d", &n);
//////	if (n > 0)
//////	{
//////		for (i = 0; i < n; i++)
//////		{
//////			scanf("%f", &score);
//////			sum += score;
//////		}
//////		double average = sum / n;
//////		printf("the number of students:the scores:average=%.2lf", average);
//////	}
//////	else
//////	{
//////		printf("the number of students:the scores:average=0.00");
//////	}
//////
//////}
////
//////int main()
//////{
//////	float x = 0;
//////	scanf("%f", &x);
//////	printf("%.3lf", sqrt(x) + pow(2, x) + log10(x));
//////	return 0;
//////}
////
////
//////int main()
//////{
//////	printf("\"\\(^_^)/\"");
//////	return 0;
//////}
////
//////int main()
//////{
//////	char n;
//////	scanf("%c", &n);
//////	int m = n - 48;
//////	printf("%d %d", n, m*m);
//////	return 0;
//////}
////
//////int main()
//////{
//////	char n;
//////	scanf("%c", &n);
//////	printf("%c %c %c", n + 31, n + 32, n + 33);
//////	return 0;
//////}
////
////
//////int main()
//////{
//////	float r = 0;
//////	scanf("%f", &r);
//////	printf("v=%.2lf", 4.0 / 3 * PI * r * r * r);
//////	return 0;
//////}
////
////
//////int main()
//////{
//////	int a, b, q, r;
//////	scanf("%d%d", &a, &b);
//////	q = a / b;
//////	r = a % b;
//////	printf("%d=%d*%d+%d", a, b, q, r);
//////	return 0;
//////}
////
//////int main()
//////{
//////	int a, b;
//////	scanf("%d/%d", &a, &b);
//////	printf("%.2lf", a * 1.0 / b);
//////	return 0;
//////}
////
////
//////void change(int* a, int* b)
//////{
//////    int tmp = *a;
//////    *a = *b;
//////    *b = tmp;
//////}
//////int main()
//////{
//////    int a, b, c, t;
//////    scanf("%d %d %d", &a, &b, &c);
//////    /* 请编写代码，实现变量a、b、c交换值 */
//////    /********** Begin **********/
//////    change(&a, &b);
//////    change(&a ,&c);
//////
//////
//////    /*********** End ***********/
//////    printf("%d,%d,%d\n", a, b, c);
//////    return 0;
//////}
////
////
////
//////int main()
//////{
//////	int x = 0;
//////	scanf("%d", &x);
//////	printf("%d", 20000 - x);
//////	return 0;
//////}
////
////
//////int main()
//////{
//////	printf("   *\n");
//////	printf("  ***\n");
//////	printf(" *****\n");
//////	printf("*******\n");
//////	return 0;
//////}
////
////
//////
//////int main()
//////{
//////    int i = 0;
//////    int j = 0;
//////    int n = 0;
//////    int o = 0;
//////    int line = 0;
//////    scanf("%d", &n);
//////    for (i = 0; i < n; i++)
//////    {
//////        for (j = 0; j < 3; j++)
//////        {
//////            for (int o = 0; o < 3 * n - line - 1; o++)
//////            {
//////                printf(" ");
//////            }
//////            for (int m = 0; m < i + 1; m++)
//////            {
//////                for (o = 0; o < j + 1; o++)
//////                {
//////                    printf("* ");
//////                }
//////                for (o = 0; o < 4 - 2 * j; o++)
//////                {
//////                    printf(" ");
//////                }
//////            }
//////            printf("\n");
//////
//////            line++;
//////        }
//////    }
//////    for (i = 0; i < n; i++)
//////    {
//////        for (j = 0; j < 3 * n - 1; j++)
//////        {
//////            printf(" ");
//////        }
//////        printf("*");
//////        printf("\n");
//////    }
//////}
////
//////int main()
//////{
//////	char c1;
//////	//测试注释是否被替换成空格
//////	scanf("%c", &c1);
//////	printf("%c\n", c1);
//////	return 0;
//////}
////
////
////
//
//
//#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
//void qsort (void* base, size_t num, size_t size,int (*compar)(const void*,const void*));
//qsort库函数的作用：排序多类型的数组
//参数*base->需要排序的数组
//参数num数组的字节大小
//参数size数组元素的字节大小
//参数*compar函数指针，有两个参数为快速排序中两个排序数，返回值为int型，规定如果前面大返回值大于0，相等返回值=0，反之小于0



//qsort 的使用
void print(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
void print_f(float arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%lf ", arr[i]);
	}
}
//升序
int  com_int_h(const void* x, const void* y)
{
	return (*(int*)x - *(int*)y);
}
//降序
int  com_int_l(const void* x, const void* y)
{
	return (*(int*)y - *(int*)x);
}

//void test1()
//{
//	int arr[10]={1,2,6,7,4,5,8,9,3,0};
//	int sz=sizeof(arr)/sizeof(arr[0]);
//	int size=sizeof(arr[0]);
//	qsort(arr,sz,size,com_int_l);
//	print(arr,sz);
//}
struct Std
{
	char name[20];
	int age;
};
struct Std s1 = { "zhangsan",18 };
struct Std s2 = { "lisi",30 };
struct Std s3 = { "wangwu",22 };
int com_struct_name(const void* x, const void* y)
{
	return(strcmp(((struct Std*)x)->name, ((struct Std*)y)->name));
}

void print_struct_name(struct Std arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s\n", arr[i].name);
	}
}
void test2()
{
	struct Std arr[] = { {"zhangsan",18},{"lisi",30},{"wangwu",22} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int size = sizeof(arr[0]);
	qsort(arr, sz, size, com_struct_name);
	print_struct_name(arr, sz);

}
int com_struct_age(const void* e1, const void* e2)
{
	return (((struct Std*)e1)->age - ((struct Std*)e2)->age);
}
void test3()
{
	struct Std arr[] = { {"zhangsan",18},{"lisi",30},{"wangwu",22} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int size = sizeof(arr[0]);
	qsort(arr, sz, size, com_struct_age);
	//不再打印，在调试窗口中观察。
	//print_struct_name(arr,sz);
}
int com_float(const void* x, const void* y)
{
	return *(float*)x - *(float*)y;
}
void test4()
{
	float arr[] = { 4.14,2.3,3.3,5,6 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int size = sizeof(arr[0]);
	qsort(arr, sz, size, com_float);
	print_f(arr, sz);
}
void bubble_sort(int arr[], int sz)
{
	int i = 0;
	for (i = 0; i < sz - 1; i++)
	{
		bool flag = true;
		int j = 0;
		for (j = 0; j < sz - 1 - i; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
				flag = false;
			}
		}
		if (flag)
		{
			break;
		}
	}
}
void test5()
{
	int arr[10] = { 1,9,4,7,8,5,6,3,2,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int size = sizeof(arr[0]);
	bubble_sort(arr, sz);
}

//swap函数依旧是一个字节一个字节交换
void Swap(char* buf1, char* buf2, int size)
{
	int i = 0;
	for (i = 0; i < size; i++)
	{
		char tmp = *buf1;
		*buf1 = *buf2;
		*buf2 = tmp;
		buf1++;
		buf2++;
	}
}
void bubble_sort_qsort(void* base, size_t num, size_t size, int (*compar)(const void*, const void*))
{
	//冒泡排序的写法只能对整形进行排序
	//根据qsort的参数可以简单的将冒泡改为多类型

	//分析：参数的修改:这就要按照给定的参数进行设置，每个参数都有自己的意义
	//for(i=0;i<sz-1;i++)
	//首先都是对数组的排序，这个循环多少趟及产生下标肯定不能省吧。
	int i = 0;
	for (i = 0; i < num - 1; i++)
	{
		int j = 0;
		for (j = 0; j < num- 1 - i; j++)
		{
			//既然传过来的void指针类型那么不能直接进行解引用操作，怎么比较呢？
			//强转char*类型，逐个字节逐个字节比较
			//利用已经给定的比较函数，给他传递由j下标锁定的元素（地址），以及每个元素的字节长
			if (compar((char*)base + j * size, (char*)base + (j + 1) * size)> 0)
			{
				Swap((char*)base + j * size, (char*)base + (j + 1) * size,size);
			}
		}
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
}
void test6()
{
	//int 数组为例
	float base2[] = { 1,9,4,7,8,5,6,3,2,0 };
	struct Std base[] = { {"zhangsan",18},{"lisi",30},{"wangwu",22} };
	int sz = sizeof(base) / sizeof(base[0]);
	int size = sizeof(base[0]);
	//bubble_sort_qsort(base, sz,size, com_float);
	bubble_sort_qsort(base, sz,size, com_struct_name);


}
int main()
{
	//测试排序整形（大小）
	//test1();
	//测试排序字符串（字母序大小）以结构体变量为例
	//test2();
	//测试对结构体变量中数组排序;
	//test3();
	//测试对浮点数的排序
	//test4();


	//对qsort库函数的模拟实现（bubble_sort改装）
	//首先写一下冒泡排序
	//test5();


	//冒泡——改
	test6();
	return 0;
}
//
//
//


//
//int main()
//{
//	int a = 10;
//	void* pa = &a;
//	*pa;
//	pa + 1;                        
//	return 0;
//}
//


//int main()
//{
//	
//		int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
//		int* p = &arr[0];
//		int i = 0;
//		int sz = sizeof(arr) / sizeof(arr[0]);
//		for (i = 0; i < sz; i++)
//		{
//			printf("%d ", *(p + i));
//		}
//	return 0;
//}


//int main()
//{
//	int n = 0;
//	printf("输入参与选举的人数:\n");
//	while (scanf("%d", &n) != EOF)
//	{
//		printf("请依次输入候选人:\n");
//		int addt[4] = { 0 };
//		for(int i=0;i<n;i++)
//		{
//			char vote = 0;
//			scanf("%S", &vote);
//			int m = vote - 64;
//			switch (m)
//			{
//			case 1:
//				addt[0]++;
//				break;
//			case 2:
//				addt[1]++;
//				break;
//			case 3:
//				addt[2]++;
//				break;
//			case 4:
//				addt[3]++;
//				break;
//			default:
//				break;
//			}
//		}
//		printf("A:%d票\n", addt[0]);
//		printf("B:%d票\n", addt[1]);
//		printf("C:%d票\n", addt[2]);
//		printf("D:%d票\n", addt[3]);
//	}
//	return 0;
//}
//int my_strlen( char* s)
//{
//	char* begin = s;
//	while (*s != '\0')
//	{
//		s++;
//	}
//	return s - begin;
//}
//int main()
//{ 
//	printf("%d\n", my_strlen("abcde"));
//}

//
//int main()
//{
//	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
//	int* p = &arr[0];
//	int sz = sizeof(arr) / sizeof(arr[0]);
//	while (p < arr + sz)
//	{
//		printf("%d ", *p);
//		p++;;
//	}
//	return 0;
//	return 0;
//}